<template>
  <div class="admin-container">
    <el-container>
      <!-- 侧边栏导航 -->
      <el-aside width="220px">
        <div class="admin-logo">
          <h2>标王资料网 - 管理后台</h2>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          router
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/admin/dashboard">
            <el-icon><PieChart /></el-icon>
            <span>数据概览</span>
          </el-menu-item>
          
          <el-submenu index="1">
            <template #title>
              <el-icon><Folder /></el-icon>
              <span>资料管理</span>
            </template>
            <el-menu-item index="/admin/materials/list">资料列表</el-menu-item>
            <el-menu-item index="/admin/materials/categories">分类管理</el-menu-item>
            <el-menu-item index="/admin/materials/tags">标签管理</el-menu-item>
          </el-submenu>
          
          <el-submenu index="2">
            <template #title>
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/admin/users/list">用户列表</el-menu-item>
            <el-menu-item index="/admin/users/vip">VIP管理</el-menu-item>
          </el-submenu>
          
          <el-submenu index="3">
            <template #title>
              <el-icon><Coin /></el-icon>
              <span>财务管理</span>
            </template>
            <el-menu-item index="/admin/finance/recharges">充值记录</el-menu-item>
            <el-menu-item index="/admin/finance/consumptions">消费记录</el-menu-item>
          </el-submenu>
          
          <el-menu-item index="/admin/logs">
            <el-icon><Document /></el-icon>
            <span>操作日志</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-container>
        <el-header>
          <div class="admin-header">
            <div class="left">
              <el-breadcrumb separator="/">
                <el-breadcrumb-item 
                  v-for="item in breadcrumbs" 
                  :key="item.path"
                  :to="item.path"
                >
                  {{ item.meta.title }}
                </el-breadcrumb-item>
              </el-breadcrumb>
            </div>
            <div class="right">
              <el-dropdown>
                <span class="admin-user">
                  <el-avatar :size="32" :src="userStore.user?.avatar" />
                  {{ userStore.user?.username }}
                  <el-icon><ArrowDown /></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>
        
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
import { 
  PieChart, Folder, User, Coin, Document, ArrowDown 
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const activeMenu = ref('')
const breadcrumbs = ref([])

// 更新面包屑导航
const updateBreadcrumbs = () => {
  const matched = route.matched.filter(item => item.meta?.title)
  breadcrumbs.value = matched
}

// 更新激活菜单
const updateActiveMenu = () => {
  const { path } = route
  activeMenu.value = path
}

// 退出登录
const logout = () => {
  userStore.logout()
  router.push('/login')
}

// 监听路由变化
watch(route, () => {
  updateActiveMenu()
  updateBreadcrumbs()
}, { immediate: true })
</script>

<style scoped>
.admin-container {
  height: 100vh;
}

.admin-logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background-color: #2b2f3a;
}

.el-aside {
  background-color: #304156;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
}

.admin-header {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.admin-user {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.admin-user .el-avatar {
  margin-right: 10px;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style>